Dreamweaver Article

How to Develop an Online Shopping Cart (Part Three)


Table of Contents

  1. Introduction
  2. Example Application Setup and the Database
  3. Application.cfm
  4. The Home Page
  5. The Check Out Page
  6. The Process Order Page
  7. The Administration Page

Administration

Finally, let's take a quick look at the administration section of the shopping cart application. I won't go into much detail here. In it's current state, the application just shows the orders and the customers using standard Dreamweaver Dynamic Table Server behaviors. Something I will guide you though is the how to secure the admin section of the application from unauthorized users. Obviously, you don't want just anyone looking at the sales and customer information, so I set up a simple but very effective user authorization system.

admin/Application.cfm

Since the administration section of the application has its own global parameters, I use another Application.cfm file to set these. Using multiple Application.cfm files in a single application works fine for ColdFusion due to the fact that the ColdFusion server will look up the directory tree from the current open file to find the first Application.cfm file it comes to and then uses that Application.cfm file. Since this file is in the admin section's root, this is the Application.cfm file that the ColdFusion server will use for pages in the admin directory of the application.

Open the Application.cfm file in the admin directory in Code view. Notice once again that sessions are enabled, and you'll see a section of code where the user is logged out. See below.

<!--- If the admin user clicks "Log Out" kill "Logged In" session. --->
<cfif IsDefined ('URL.logout')> 
<cflock timeout="10" throwontimeout="NO" type="EXCLUSIVE" scope="SESSION">
<cfset StructDelete(session, "LoggedIn") >
</cflock>
</cfif>
If a user clicks the Log Out link anywhere in the admin section,, their authorization session will be deleted and they will be returned to the log on page.

admin/index.cfm

Open the index.cfm page in the admin directory. Here the user is presented with a log on form. When a user enters a username and password and submits the form, the form action submits back to this same page and invokes the authorization code found on lines 18 through 34 as shown below.

<!--- If Form was submitted Log in user ---> 
<cfif IsDefined ('FORM.Name')> 
<!--- Run query to check username and password ---> 
<CFQUERY NAME="getAuth" DATASOURCE="DevNetExample"> 
SELECT adminID 
FROM adminUsers 
WHERE uName = '#FORM.Name#' 
AND uPassword = '#FORM.Password#' 
</CFQUERY> 
<!--- If Log on was good, set as authorized ---> 
<cfif getAuth.RecordCount EQ 1 > 
<cflock timeout="9" throwontimeout="no" type="exclusive" scope="session"> 
<cfset Session.LoggedIn = getAuth.adminID> 
</cflock> 
<!--- Go To Admin Home ---> 
<cflocation url="adminHome.cfm" addtoken="no"> 
</cfif> 
</cfif>

If the username and password used is found in the AdminUsers table, a logged in session is set and the user is taken to the administration home page (adminHome.cfm). If no match is found, nothing happens and the user is left on this page.

inc_AuthAdmin.cfm

One other file I will take a moment to discuss is the AuthAdmin server-side include file. This simple include file is placed at the very top of every page in the Admin directory of the application.

<cflock timeout="9" throwontimeout="no" type="readonly" scope="session"> 
<cfif NOT IsDefined ('Session.LoggedIn')> 
<cflocation url="AdminHome.cfm" addtoken="no"> 
</cfif> 
</cflock>

The code shown here checks to see it the user requesting the page is logged in and authorized to see the page. If not, the user is redirected to the log on page (admin/index.cfm) and none of that page is shown or executed. As long as this include is at the top of every page in the admin directory of the application, no one will be able to see any of the Admin data without first logging in.

This brings us to the end of our three-part series. I hope you've enjoyed it. Be sure to take the time to peruse through the code of the example application and look at the code comments. Although this is a very simple application, all the basic elements of an e-commerce application are there with the exception of the secure socket layer (SSL) features mentioned in parts one and two. Combined with the principles covered in the first two articles, you'll be well on your way to being able to develop solid, functional e-commerce applications.

If you have any comments or questions, please feel free to drop me a line!